package com.yk.faceapplication.activity;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.allinpay.xed.R;
import com.android.volley.VolleyError;
import com.yk.faceapplication.callback.RequestCallback;
import com.yk.faceapplication.http.RequestUrl;
import com.yk.faceapplication.util.ActivityUtil;
import com.yk.faceapplication.util.FormatUtil;
import com.yk.faceapplication.util.ImageUtil;
import com.yk.faceapplication.util.RequestUtil;

import org.json.JSONObject;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yk_2018/1/14_11:35
 */

public class AddBillActivity extends BaseActivity {
    private EditText userNameET, billNameET, moutnAmountET, totalNumberET, currentNumberET;
    private ImageView productIconIV;
    private TextView productNameTV, timeTV;
    private RelativeLayout repaymentLayout;
    private Button commitBtn;
    final int DATE_DIALOG = 1;
    int mYear, mMonth, mDay;
    private Dialog dialog;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        productIconIV = (ImageView) findViewById(R.id.pro_icon_iv);
        productNameTV = (TextView) findViewById(R.id.pro_name_tv);
        timeTV = (TextView) findViewById(R.id.time_tv);
        repaymentLayout = (RelativeLayout) findViewById(R.id.repayment_layout);
        productNameTV.setText(getIntent().getStringExtra("name"));
        String icon = getIntent().getStringExtra("icon");
        ImageUtil.displayImage(this, icon, productIconIV);

        userNameET = (EditText) findViewById(R.id.user_name);
        billNameET = (EditText) findViewById(R.id.name);
        moutnAmountET = (EditText) findViewById(R.id.month_amount);
        totalNumberET = (EditText) findViewById(R.id.total_qishu);
        currentNumberET = (EditText) findViewById(R.id.current_qishu);
        commitBtn = (Button) findViewById(R.id.commit_btn);
        commitBtn.setOnClickListener(this);
        repaymentLayout.setOnClickListener(this);
        final Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);

        setTitle("添加帐单");



        moutnAmountET.addTextChangedListener(new TextWatcher()
        {
            public void afterTextChanged(Editable edt)
            {
                String temp = edt.toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0) return;
                if (temp.length() - posDot - 1 > 2)
                {
                    edt.delete(posDot + 3, posDot + 4);
                }
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.commit_btn:
                //                if (ActivityUtil.editIsNull(userNameET)) {
                //                    Toast.makeText(this, "请输入正确的用户名", Toast.LENGTH_SHORT).show();
                //                    return;
                //                }
                if (ActivityUtil.editIsNull(billNameET)) {
                    Toast.makeText(this, "请输入正确的帐单名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (timeTV.getText().toString().trim().equals("")) {
                    Toast.makeText(this, "请输入正确的还款日期", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (ActivityUtil.editIsNull(moutnAmountET)) {
                    Toast.makeText(this, "请输入正确的贷款金额", Toast.LENGTH_SHORT).show();
                    return;
                }
                //                if (ActivityUtil.editIsNull(totalNumberET)) {
                //                    Toast.makeText(this, "请输入正确的总期数", Toast.LENGTH_SHORT).show();
                //                    return;
                //                }
                //                if (ActivityUtil.editIsNull(currentNumberET)) {
                //                    Toast.makeText(this, "请输入正确的当前期数", Toast.LENGTH_SHORT).show();
                //                    return;
                //                }
                addBills();
                break;
            case R.id.repayment_layout:
                showDialog(DATE_DIALOG);
                break;
        }
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG:
                return new DatePickerDialog(this, mdateListener, mYear, mMonth, mDay);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener mdateListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            display();
        }
    };

    public void display() {
        timeTV.setText(new StringBuffer().append(mYear).append("-").append(mMonth + 1).append("-").append(mDay).append(" "));
    }

    private void addBills() {
        dialog = ActivityUtil.createLoadingDialog(this, "请稍候~");
        dialog.show();
        Map<String, String> form = new HashMap<String, String>();
        form.put("accountName", billNameET.getText().toString().trim());
        form.put("memberId", mApplication.getMember().getId());
        form.put("realName", mApplication.getMember().getRealName());
        form.put("identityId", mApplication.getMember().getPeople().getIdCardNo());
        form.put("mobile", mApplication.getMember().getMobile());
        form.put("loanShowId", getIntent().getStringExtra("id"));

        form.put("loanAmt", moutnAmountET.getText().toString().trim());
        form.put("repaymentDate", FormatUtil.string2Long(timeTV.getText().toString().trim()+" 00:00:00")+"");
        form.put("accountMark", "M");
        form.put("loanInstalment", "1");
        form.put("orgId", getIntent().getStringExtra("orgId"));


        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {
                dialog.dismiss();
                Toast.makeText(mApplication, "帐单添加成功", Toast.LENGTH_SHORT).show();
                setResult(RESULT_OK);
                finishAct();
            }

            @Override
            public void fail(VolleyError volleyError) {
                dialog.dismiss();
            }
        };
        RequestUtil.request(this, RequestUrl.ADD_BILL + "token=" + mToken, callback, form);
    }
}
